﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Web.Mvc;
using InfoMap.Core.Interfaces;

namespace InfoMap.Core.UI
{
    /// <summary>
    /// Map helper class
    /// </summary>
    public class MapHelper
    {
        private StringBuilder htmlOut { get; set; }
        
        public IMap Map { get; set; }
        
        /// <summary>
        /// Constructor
        /// </summary>
        public MapHelper()
        {
            htmlOut = new StringBuilder();
            Map = new GoogleMap();
        }        

        /// <summary>
        /// Config the map
        /// </summary>
        /// <param name="action">Configuration code</param>
        /// <returns>Map helper</returns>
        public MapHelper Config(Action<MapHelper> action)
        {
            action.Invoke(this);
            DivTag div = new DivTag();
            div.Id = Map.DivName;
            div.Width = Map.Width;
            div.Height = Map.Height;
            htmlOut.Append(div.ToString());
            htmlOut.AppendLine(createScriptMap());
            return this;
        }

        /// <summary>
        /// Creates the map
        /// </summary>
        /// <returns>Html containing the map</returns>
        public MvcHtmlString Build()
        {
            MvcHtmlString htmlMvc = new MvcHtmlString(htmlOut.ToString());
            return htmlMvc;
        }

        private string createScriptMap()
        {
            string scriptMap = String.Format("createMap({0},{1}, {2}, {3}, '{4}');",
                                                        Map.Lat.ToString().Replace(",", "."),
                                                        Map.Lng.ToString().Replace(",", "."),
                                                        Map.Zoom,
                                                        MapUtils.GetMapType(Map.MapType),
                                                        Map.DivName);
            ScriptTag script = new ScriptTag();
            script.Content = scriptMap;
            return script.ToString();
            //createMap(-34.9168, -57.9531, 13, google.maps.MapTypeId.ROADMAP, "mapaLaPlata", "main");
        }
    }
}
